By
yusijia
Updated:
参考:http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html#!comments
通过一个对象获得完整的包名和类名
实例化Class类对象的3种方法
通过Class实例化类对象
返回一个类实现的接口
返回父类
返回他的构造方法
返回所有方法
返回类的属性
通过反射调用其他类中的方法
通过反射操作属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
| package yu;
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import java.util.Scanner;
interface hehe{ public static final String intefaceParam = "interfaceName"; }
class human{ public String fatherName; public void sayFather(){ System.out.println("我是父类"); } }
class Student extends human implements hehe{ private String name; private int id; public String asd; public String getName(){ return name; } public void setName(String name){ this.name = name; } public int getId(){ return id; } public void setId(int id){ this.id = id; } public Student() {} public Student(String name, int id){ this.name = name; this.id = id; } @Override public String toString(){ return "[" + this.name + ": " + this.id + "]"; } public void say(){ System.out.println("我是本类里的方法"); } public void say2(String a){ System.out.println("我是本类中的另一个方法"); } }
public class Lian4 { public static void main(String[] args) throws Exception { * 通过一个对象获得完整的包名和类名 * */ Student stu1 = new Student(); System.out.println(stu1.getClass().getName()); Class<?> demo1 = null; Class<?> demo2 = null; Class<?> demo3 = null; try{ demo1 = Class.forName("yu.Student"); }catch(Exception e){ e.printStackTrace(); } demo2 = new Student().getClass(); demo3 = Student.class; System.out.println("类名称 "+demo1.getName()); System.out.println("类名称 "+demo2.getName()); System.out.println("类名称 "+demo3.getName()); Class<?> studentDemo = null; try{ studentDemo = Class.forName("yu.Student"); }catch(Exception e){ e.printStackTrace(); } Student stu2 = null; try{ stu2 = (Student) studentDemo.newInstance(); }catch(InstantiationException e){ e.printStackTrace(); }catch (IllegalAccessException e) { e.printStackTrace(); } stu2.setName("yu"); stu2.setId(18); System.out.println(stu2);
for(Class<?> inte : studentDemo.getInterfaces()){ System.out.println(inte); } Class<?> up = studentDemo.getSuperclass(); System.out.println(up); Class<?> studentDemo2 = null; try{ studentDemo2 = Class.forName("yu.Student"); }catch (Exception e) { e.printStackTrace(); } Constructor<?>cons[] = studentDemo2.getConstructors(); for(Constructor<?> con : cons){ System.out.println(con); } System.out.println(); Method[] method = studentDemo2.getMethods(); for(int i = 0; i < method.length; i++){ Class<?> returnType = method[i].getReturnType(); Class<?>[] para = method[i].getParameterTypes(); int temp = method[i].getModifiers(); System.out.print(Modifier.toString(temp)+" "); System.out.print(returnType.getName()+" "); System.out.print(method[i].getName()+" "); System.out.print("("); for(int j = 0; j < para.length; ++j){ System.out.print(para[j].getName() + " " + "arg" + j); if(j < para.length - 1){ System.out.print(","); } } Class<?>[] exce = method[i].getExceptionTypes(); if(exce.length > 0){ System.out.print(") throws "); for(int k = 0; k < exce.length; ++k){ System.out.print(exce[k].getName() + " "); if(k < exce.length - 1){ System.out.print(","); } } }else{ System.out.print(")"); } System.out.println(); } System.out.println("===============本类属性所有属性========================"); Field[] field = studentDemo2.getDeclaredFields(); for (int i = 0; i < field.length; i++) { int mo = field[i].getModifiers(); String priv = Modifier.toString(mo); Class<?> type = field[i].getType(); System.out.println(priv + " " + type.getName() + " " + field[i].getName() + ";"); } System.out.println("===============实现的接口或者父类或本类的可访问属性========================"); Field[] filed1 = studentDemo2.getFields(); for (int j = 0; j < filed1.length; j++) { int mo = filed1[j].getModifiers(); String priv = Modifier.toString(mo); Class<?> type = filed1[j].getType(); System.out.println(priv + " " + type.getName() + " " + filed1[j].getName() + ";"); } Class<?> studentDemo3 = null; try{ studentDemo3 = Class.forName("yu.Student"); }catch(Exception e){ e.printStackTrace(); } try{ Method method1 = studentDemo3.getMethod("say"); method1.invoke(studentDemo3.newInstance()); Method method2 = studentDemo3.getMethod("say2", String.class); method2.invoke(studentDemo3.newInstance(), "随便"); }catch(Exception e){ e.printStackTrace(); } Object obj = studentDemo3.newInstance(); Field field1 = studentDemo3.getDeclaredField("name"); field1.setAccessible(true); field1.set(obj, "name:修改后的名字"); System.out.println(field1.get(obj)); } }
输出:
yu.Student 类名称 yu.Student 类名称 yu.Student 类名称 yu.Student [yu: 18] interface yu.hehe class yu.human public yu.Student(java.lang.String,int) public yu.Student()
public java.lang.String toString () public java.lang.String getName () public int getId () public void setName (java.lang.String arg0) public void setId (int arg0) public void say2 (java.lang.String arg0) public void say () public void sayFather () public final void wait () throws java.lang.InterruptedException public final void wait (long arg0,int arg1) throws java.lang.InterruptedException public final native void wait (long arg0) throws java.lang.InterruptedException public boolean equals (java.lang.Object arg0) public native int hashCode () public final native java.lang.Class getClass () public final native void notify () public final native void notifyAll () ===============本类属性所有属性======================== private java.lang.String name; private int id; public java.lang.String asd; ===============实现的接口或者父类或本类的可访问属性======================== public java.lang.String asd; public static final java.lang.String intefaceParam; public java.lang.String fatherName; 我是本类里的方法 我是本类中的另一个方法 name:修改后的名字
|